/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import domain.Bankaccount;
import domain.Electricitycontract;
import domain.Insurancecontract;
import domain.Invoice;
import domain.Mobilecontract;
import domain.Mortgagecontract;
import domain.Watercontract;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Alberto
 */
public class InvoiceDao {

    EntityManager em;
    EntityTransaction tx;

    public InvoiceDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Invoice getInvoice(Integer idI){
        Invoice b = (Invoice) em.createNativeQuery("select * from invoice as i where i.idInvoice='"+idI+"'", Invoice.class).getSingleResult();
        return b;
    }

    public Set<Invoice> getInvoiceBankFK(Bankaccount b){
        Set<Invoice> i = new HashSet(em.createNativeQuery("select * from invoice as i where i.bankFK='"+b.getIdBank()+"'", Invoice.class).getResultList());
        return i;
    }

    public void saveInvoice(Invoice i){
        tx.begin();
        em.persist(i);
        tx.commit();
    }

    public void removeInvoice (Invoice i){
        tx.begin();
        em.remove(i);
        tx.commit();
    }

    public List<Invoice> getInsuranceInvoiceList(Insurancecontract i){
        List<Invoice> l = em.createNativeQuery("select * from contract as c, bankaccount as b, invoice as i where c.idContract = '"+i.getIdInsuranceContract()+"' and c.bankFK = b.idBank and i.bankFK = b.idBank ", Invoice.class).getResultList();
        return l;
    }
    
    public List<Invoice> getWaterInvoiceList(Watercontract w){
        List<Invoice> l = em.createNativeQuery("select * from contract as c, bankaccount as b, invoice as i where c.idContract = '"+w.getIdWaterContract()+"' and c.bankFK = b.idBank and i.bankFK = b.idBank ", Invoice.class).getResultList();
        return l;
    }

    public List<Invoice> getMortgageInvoiceList(Mortgagecontract m){
        List<Invoice> l = em.createNativeQuery("select * from contract as c, bankaccount as b, invoice as i where c.idContract = '"+m.getIdMortgageContract()+"' and c.bankFK = b.idBank and i.bankFK = b.idBank ", Invoice.class).getResultList();
        return l;
    }

    public List<Invoice> getElectricityInvoiceList(Electricitycontract e){
        List<Invoice> l = em.createNativeQuery("select * from contract as c, bankaccount as b, invoice as i where c.idContract = '"+e.getIdElectricityContract()+"' and c.bankFK = b.idBank and i.bankFK = b.idBank ", Invoice.class).getResultList();
        return l;
    }





}
